package com.youyi.yychosesdk.models.sticker;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.ViewGroup;
import com.youyi.yychosesdk.EasyPhotos;
import com.youyi.yychosesdk.models.sticker.cache.StickerCache;
import com.youyi.yychosesdk.models.sticker.entity.TextStickerData;
import com.youyi.yychosesdk.models.sticker.listener.OnStickerClickListener;
import com.youyi.yychosesdk.models.sticker.view.BitmapSticker;
import com.youyi.yychosesdk.models.sticker.view.EditFragment;
import com.youyi.yychosesdk.models.sticker.view.TextSticker;
import com.youyi.yychosesdk.utils.bitmap.BitmapUtils;
import com.youyi.yychosesdk.utils.bitmap.SaveBitmapCallBack;
import java.util.ArrayList;
import java.util.List;

public class StickerModel {
    public static final ArrayList<TextStickerData> textDataList = new ArrayList<>();
    public List<BitmapSticker> bitmapStickers = new ArrayList();
    public BitmapSticker currBitmapSticker;
    public TextSticker currTextSticker;
    public List<TextSticker> textStickers = new ArrayList();

    public void addBitmapSticker(Context context, String str, int i, ViewGroup viewGroup) {
        if (this.bitmapStickers.size() > 0) {
            List<BitmapSticker> list = this.bitmapStickers;
            if (!list.get(list.size() - 1).isChecked) {
                List<BitmapSticker> list2 = this.bitmapStickers;
                list2.get(list2.size() - 1).delete();
            }
        }
        final BitmapSticker bitmapSticker = new BitmapSticker(context, str, i, viewGroup.getWidth() / 2, viewGroup.getHeight() / 2);
        bitmapSticker.setOnStickerClickListener(new OnStickerClickListener() {
            /* class com.youyi.yychosesdk.models.sticker.StickerModel.AnonymousClass1 */

            @Override // com.youyi.yychosesdk.models.sticker.listener.OnStickerClickListener
            public void onEditor() {
            }

            @Override // com.youyi.yychosesdk.models.sticker.listener.OnStickerClickListener
            public void onDelete() {
                StickerModel.this.bitmapStickers.remove(bitmapSticker);
            }

            @Override // com.youyi.yychosesdk.models.sticker.listener.OnStickerClickListener
            public void onTop() {
                StickerModel.this.bitmapStickers.remove(bitmapSticker);
                StickerModel.this.bitmapStickers.add(bitmapSticker);
            }

            @Override // com.youyi.yychosesdk.models.sticker.listener.OnStickerClickListener
            public void onUsing() {
                if (StickerModel.this.currBitmapSticker != null && StickerModel.this.currBitmapSticker != bitmapSticker) {
                    StickerModel.this.currBitmapSticker.setUsing(false);
                    StickerModel.this.currBitmapSticker = bitmapSticker;
                }
            }
        });
        BitmapSticker bitmapSticker2 = this.currBitmapSticker;
        if (bitmapSticker2 != null) {
            bitmapSticker2.setUsing(false);
        }
        viewGroup.addView(bitmapSticker);
        this.currBitmapSticker = bitmapSticker;
        this.bitmapStickers.add(bitmapSticker);
    }

    public void addTextSticker(Context context, final FragmentManager fragmentManager, String str, ViewGroup viewGroup) {
        if (this.textStickers.size() > 0) {
            List<TextSticker> list = this.textStickers;
            if (!list.get(list.size() - 1).isChecked) {
                List<TextSticker> list2 = this.textStickers;
                list2.get(list2.size() - 1).delete();
            }
        }
        final TextSticker textSticker = new TextSticker(context, str, viewGroup.getWidth() / 2, viewGroup.getHeight() / 2);
        textSticker.setOnStickerClickListener(new OnStickerClickListener() {
            /* class com.youyi.yychosesdk.models.sticker.StickerModel.AnonymousClass2 */

            @Override // com.youyi.yychosesdk.models.sticker.listener.OnStickerClickListener
            public void onDelete() {
                StickerModel.this.textStickers.remove(textSticker);
            }

            @Override // com.youyi.yychosesdk.models.sticker.listener.OnStickerClickListener
            public void onEditor() {
                EditFragment.show(fragmentManager, textSticker);
            }

            @Override // com.youyi.yychosesdk.models.sticker.listener.OnStickerClickListener
            public void onTop() {
                StickerModel.this.textStickers.remove(textSticker);
                StickerModel.this.textStickers.add(textSticker);
            }

            @Override // com.youyi.yychosesdk.models.sticker.listener.OnStickerClickListener
            public void onUsing() {
                if (StickerModel.this.currTextSticker != null && StickerModel.this.currTextSticker != textSticker) {
                    StickerModel.this.currTextSticker.setUsing(false);
                    StickerModel.this.currTextSticker = textSticker;
                }
            }
        });
        BitmapSticker bitmapSticker = this.currBitmapSticker;
        if (bitmapSticker != null) {
            bitmapSticker.setUsing(false);
        }
        viewGroup.addView(textSticker);
        this.currTextSticker = textSticker;
        this.textStickers.add(textSticker);
    }

    public void save(Activity activity, ViewGroup viewGroup, View view, int i, int i2, String str, String str2, boolean z, SaveBitmapCallBack saveBitmapCallBack) {
        Bitmap bitmap;
        BitmapSticker bitmapSticker = this.currBitmapSticker;
        if (bitmapSticker != null && bitmapSticker.isUsing()) {
            this.currBitmapSticker.setUsing(false);
        }
        TextSticker textSticker = this.currTextSticker;
        if (textSticker != null && textSticker.isUsing()) {
            this.currTextSticker.setUsing(false);
        }
        for (BitmapSticker bitmapSticker2 : this.bitmapStickers) {
            if (bitmapSticker2.isUsing()) {
                bitmapSticker2.setUsing(false);
            }
        }
        for (TextSticker textSticker2 : this.textStickers) {
            if (textSticker2.isUsing()) {
                textSticker2.setUsing(false);
            }
        }
        Bitmap createBitmap = Bitmap.createBitmap(viewGroup.getWidth(), viewGroup.getHeight(), Bitmap.Config.ARGB_8888);
        viewGroup.draw(new Canvas(createBitmap));
        Bitmap createBitmap2 = Bitmap.createBitmap(createBitmap, view.getLeft(), view.getTop(), view.getWidth(), view.getHeight());
        BitmapUtils.recycle(createBitmap);
        if (view.getWidth() > i || view.getHeight() > i2) {
            Bitmap createScaledBitmap = Bitmap.createScaledBitmap(createBitmap2, i, i2, true);
            BitmapUtils.recycle(createBitmap2);
            bitmap = createScaledBitmap;
        } else {
            bitmap = createBitmap2;
        }
        EasyPhotos.saveBitmapToDir(activity, str, str2, bitmap, z, saveBitmapCallBack);
    }

    public void setCanvasSize(final Bitmap bitmap, final ViewGroup viewGroup) {
        if (viewGroup.getMeasuredWidth() == 0) {
            viewGroup.post(new Runnable() {
                /* class com.youyi.yychosesdk.models.sticker.StickerModel.AnonymousClass3 */

                public void run() {
                    StickerModel.this.setSize(bitmap, viewGroup);
                }
            });
        } else {
            setSize(bitmap, viewGroup);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void setSize(Bitmap bitmap, ViewGroup viewGroup) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int measuredWidth = viewGroup.getMeasuredWidth();
        int measuredHeight = viewGroup.getMeasuredHeight();
        float f = (float) measuredWidth;
        float f2 = (float) width;
        float f3 = f / f2;
        float f4 = (float) measuredHeight;
        float f5 = (float) height;
        float f6 = f4 / f5;
        ViewGroup.LayoutParams layoutParams = viewGroup.getLayoutParams();
        if (width >= height) {
            layoutParams.width = measuredWidth;
            layoutParams.height = (int) (f3 * f5);
        } else {
            layoutParams.width = (int) (f6 * f2);
            layoutParams.height = measuredHeight;
        }
        if (layoutParams.width > measuredWidth) {
            float f7 = f / ((float) layoutParams.width);
            layoutParams.width = measuredWidth;
            layoutParams.height = (int) (((float) layoutParams.height) * f7);
        }
        if (layoutParams.height > measuredHeight) {
            float f8 = f4 / ((float) layoutParams.height);
            layoutParams.height = measuredHeight;
            layoutParams.width = (int) (((float) layoutParams.width) * f8);
        }
        viewGroup.setLayoutParams(layoutParams);
    }

    public void release() {
        StickerCache.get().clear();
    }
}
